package com.linewell.dao;

import com.linewell.entity.Administrator;
import com.linewell.entity.UserInfo;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @Title: zmt
 * @description:
 * @author: hyuhuang@linewell.com
 * @since:2018/1/16 10:50
 */
public interface UserInfoMapper {
    @Select("select * from userinfo where punster_name = #{punsterName} and pwd = #{pwd}")
    List<UserInfo> checkNamePwd(@Param("punsterName") String punsterName,@Param("pwd") String pwd);

    @Insert("insert into userinfo(punster_name,nick_name,pwd,state,email) values (#{punsterName},#{nickName},#{pwd},0,#{email})")
    boolean savaUserInfo(UserInfo userInfo);

    @Select("select * from userinfo where punster_name = #{punsterName}")
    List<UserInfo> checkPunsterName(@Param("punsterName") String punsterName);

    @Update("update userinfo set picture=#{picture} where user_id=${userId}")
    boolean uploadUserPicture(@Param("picture") String picture,@Param("userId") Long userId);

    @Select("select * from userinfo order by (fans_num) DESC Offset (${page}-1)*10 Row Fetch Next 10 Rows Only")
    List<UserInfo> getHotUser(@Param("page")int page);

    @Select("select * from administrator where admin = #{punsterName} and password = #{password}")
    Administrator checkAdmin(@Param("punsterName")String punsterName, @Param("password")String password);

    @Update("update userinfo set nick_name = #{nickName},sex = #{sex},birthday = #{birthday},email = #{userEmail},my_evaluation = #{myself} where user_id = ${userId}")
    boolean updataUserInfo(@Param("userId")String userId, @Param("nickName")String nickName, @Param("sex")String sex, @Param("birthday")String birthday, @Param("userEmail")String userEmail, @Param("myself")String myself);

    @Select("select * from userinfo where user_id = ${userId} AND pwd = '${oldPwd}'")
    List<UserInfo> checkPwd(@Param("userId")Long userId, @Param("oldPwd")String oldPwd);

    @Update("update userinfo set pwd = #{newPwd} where user_id = ${userId}")
    boolean updatePwd(@Param("userId")Long userId, @Param("newPwd")String newPwd);

    @ResultMap("com.linewell.dao.UserInfoMapper.UserInfo")
    @Select("select user_id,punster_name,nick_name,sex,state,picture,focus_num,fans_num,birthday,email,my_evaluation from userinfo order by user_id Desc Offset (${page}-1)*${limit} Row Fetch Next ${limit} Rows Only")
    List<UserInfo> getUsers(@Param("page") int page,@Param("limit")int limit);

    @Select("select count(*) from userinfo")
    int getUserCount();

    @ResultMap("com.linewell.dao.UserInfoMapper.UserInfo")
    @Select("SELECT user_id,punster_name,nick_name,sex,state,picture,focus_num,fans_num,birthday,email,my_evaluation FROM " +
            "userinfo where punster_name = #{keyword}")
    List<UserInfo> getUsersByPunsterName(@Param("keyword")String keyword);

    @Delete("delete from userinfo where user_id in (${userIds})")
    boolean deleteAllUser(@Param("userIds")String userIds);

    @Delete("delete from userinfo where user_id = ${userId}")
    boolean deleteUserByUserId(@Param("userId")Long userId);

    @Update("update userinfo set state = ${state} where user_id = ${userId}")
    boolean updateUserState(@Param("userId")Long userId, @Param("state")int state);

    @Update("update userinfo set ${field} = #{value} where user_id = ${userId}")
    boolean updateUser(@Param("field")String field, @Param("value")String value,@Param("userId")Long userId);

    @Select("SELECT user_id,punster_name,nick_name,sex,state,picture,focus_num,fans_num,birthday,email,my_evaluation FROM " +
            "userinfo where user_id = ${userId}")
    UserInfo getUserById(@Param("userId")Long userId);

    @Select("select * from userinfo where punster_name = #{punsterName} and email = #{email}")
    UserInfo checkEmail(@Param("punsterName") String punsterName,@Param("email") String email);

    @Select("select * from userinfo where user_id = ${userId}")
    UserInfo getUserByUserId(@Param("userId") Long userId);
}
